var app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    detailAddress: '',
    purchaseWeight: '', //今日收货量
    purchasePrice: '', //今日收货金额
    avgprice: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  //扫一扫
  onScan: function() {
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
      success: res => {
        console.log(res.result)
        wx.request({
          method: 'POST',
          url: app.preUrl + 'sweepSupplier',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded;'
          },
          data: {
            id: res.result,
            packerId: app.globalData.userId
          },
          success: function(res1) {
            console.log(res1)
            if (res1.data.code == 100) {
              wx.navigateTo({
                url:
                  'orderByHand?supplierId=' +
                  res1.data.result.userId +
                  '&name=' +
                  res1.data.result.name +
                  '&supplierPackerId=' +
                  res1.data.result.id
              })
            } else {
              wx.showModal({
                title: '温馨提示',
                content: res1.data.message
              })
            }
          },
          fail: function(error) {
            console.log('error', error)
          }
        })
      },
      fail: res => {
        console.log('fail', res)
        wx.showModal({
          title: '不符合该系统规定的二维码！',
          confirmColor: '#56BDE0',
          showCancel: false
        })
      }
    })
  },

  //财务报表
  goFinancal: function() {
    wx.navigateTo({
      url: '../financial/financial'
    })
  },

  //验货定价
  goPriced: function() {
    wx.navigateTo({
      url: 'price_list'
    })
  },
  //订单结算
  goOrderSettlement: function() {
    wx.navigateTo({
      url: 'order_settlement'
    })
  },
  //所有订单
  goOrder: function() {
    wx.navigateTo({
      url: 'order'
    })
  },
  //供应商
  goSupplier: function() {
    wx.navigateTo({
      url: 'supplier'
    })
  },
  //手工录单
  goOrderByHand: function() {
    wx.navigateTo({
      url: 'orderByHand'
    })
  },
  //出库登记
  outgoingRegistration: function() {
    wx.navigateTo({
      url: 'outgoingRegistration_list'
    })
  },
  //到厂数据
  goFactoryData: function() {
    wx.navigateTo({
      url: 'factoryData'
    })
  },
  //销货对账单
  goSalesStatement: function() {
    wx.navigateTo({
      url: 'sales_statement'
    })
  },
  //自提销货
  goZtxh() {
    wx.navigateTo({
      url: 'ztxh'
    })
  },
  //库存统计
  gostoreStatistics: function() {
    wx.navigateTo({
      url: 'store_statistics'
    })
  },
  //库存盘点
  goStockChecked: function() {
    wx.navigateTo({
      url: 'stock_checked'
    })
  },
  //风险设置
  goriskSetting: function() {
    wx.navigateTo({
      url: 'riskSetting'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var value = wx.getStorageSync('userId')
    console.log(value)
    if (value) {
      app.globalData.userId = value
    } else {
      wx.reLaunch({ url: '../registerOrLogin/login' })
      return
    }
    this.getInit()
  },
  getInit() {
    wx.showLoading({ title: '加载中' })
    let that = this
    Promise.all([this.getUserId(), this.getTodayData()])
      .then(res => {
        wx.hideLoading()
        let [userInfo, todayData] = res
        var detailAddress =
          userInfo.province + userInfo.city + userInfo.area + userInfo.address
        if (detailAddress.length > 18) {
          detailAddress = detailAddress.substring(0, 14) + '...'
        }
        that.setData({ detailAddress: detailAddress })
        wx.setNavigationBarTitle({ title: userInfo.siteName })
        ///
        if (todayData.purchasePrice == 0 || todayData.purchaseWeight == 0) {
          that.setData({
            purchaseWeight: Math.round(todayData.purchaseWeight),
            purchasePrice: Math.round(todayData.purchasePrice),
            avgprice: 0.0
          })
        } else {
          that.setData({
            purchaseWeight: Math.round(todayData.purchaseWeight),
            purchasePrice: Math.round(todayData.purchasePrice),
            avgprice: (
              todayData.purchasePrice / todayData.purchaseWeight
            ).toFixed(2)
          })
        }
      })
      .catch(res => {
        console.log(res)
        //服务器状态异常提示
        wx.showModal({
          title: '提示',
          content: '服务器异常',
          confirmColor: '#56BDE0',
          showCancel: false
        })
      })
  },
  getUserId() {
    var that = this
    return new Promise(function(resolve, reject) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'findByUserId',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId
        },
        success: function(res) {
          if (res.data.code == 100) {
            let data = res.data.result
            resolve(data)
          } else {
            console.log(1122)
            reject('服务器状态异常')
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    })
  },
  getTodayData() {
    let that = this
    return new Promise(function(resolve, reject) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'getTodayData',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId
        },
        success: function(res) {
          if (res.data.code == 100) {
            let data = res.data.result
            resolve(data)
          } else {
            console.log(11)
            reject('服务器状态异常')
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
